package com.leilei.dao.impl;

import com.leilei.dao.ICommodityColor;
import com.leilei.dao.ICommodityDao;
import com.leilei.dao.ICommodityVersion;
import com.leilei.dao.IHisShoppingCustomDao;
import com.leilei.entity.HisShoppingCustom;
import com.leilei.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class HisShoppingCustomDaoImpl implements IHisShoppingCustomDao {

    private QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());
    private ICommodityDao commodityDao = new CommodityDaoImpl();
    private ICommodityVersion commodityVersion = new CommodityVersionImpl();
    private ICommodityColor commodityColor = new CommodityColorImpl();

    @Override
    public List<HisShoppingCustom> selectUserHisShoppingCustom(int uid) {

        String sql = "select scid, uid, pid, pcid, pvid, did, buycount, buytime, isbuy from shopcustom where uid = ?";

        try {

            List<HisShoppingCustom> query = queryRunner.query(sql,
                    new BeanListHandler<HisShoppingCustom>(HisShoppingCustom.class), uid);

            for (HisShoppingCustom hs : query) {
                hs.setCommodity(commodityDao.selectCommodityById(hs.getPid()));
                hs.setCommodityVersion(commodityVersion.getCommodityVersionByPvid(hs.getPvid()));
                hs.setCommodityColor(commodityColor.selectCommodityColorByPcid(hs.getPcid()));
            }

            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }


}
